package com.ys.service.amazonorder.impl;

import com.ys.entity.amazonorder.TcoRankShopByAmountRmb;
import com.ys.mapper.amazonorder.AmazonorderMapper;
import com.ys.service.amazonorder.TcoRankShopByAmountRmbService;
import com.ys.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


@Service("tcoRankShopByAmountRmbService")
public class TcoRankShopByAmountRmbServiceImpl implements TcoRankShopByAmountRmbService {
    private static final Logger logger = LoggerFactory.getLogger(TcoRankShopByAmountRmbServiceImpl.class);

	@Autowired
	private AmazonorderMapper amazonorderMapper;


	@Override
	@Transactional
	public void addTcoRankShopByAmountRmb() {

		try {
			logger.info("更新店铺每日店铺根据订单金额倒序的排名信息****开始");
			//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			for(int i=1;i<=14;i++){
				//获取14天前时间日期
				Calendar calendar = Calendar.getInstance();
				calendar.add(Calendar.DAY_OF_MONTH, -i);
				//String agoDay = sdf.format(calendar.getTime());
				countRank(calendar.getTime());
			}
			logger.info("更新店铺每日店铺根据订单金额倒序的排名信息****完成");
		}catch (Exception e){
			logger.error("更新店铺每日店铺根据订单金额倒序的排名信息****失败!!!!",e.getMessage(),e);
		}
		
	}


	@Override
	@Transactional
	public void addTcoRankShopByAmountRmbByTime(String time) {
		try {
			logger.info("更新店铺每日店铺根据订单金额倒序的排名信息****开始");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date date = sdf.parse(time);
			countRank(date);
			logger.info("更新店铺每日店铺根据订单金额倒序的排名信息****完成");
		}catch (Exception e){
			logger.error("更新店铺每日店铺根据订单金额倒序的排名信息****失败!!!!",e.getMessage(),e);
		}
	}


	public void countRank(Date now) throws Exception{
		String countDate =  DateUtil.getYYYY_MM_DD(now);
		logger.info("更新"+countDate+"排名信息");
		//今日排名
		String dayBegin = countDate;
		String dayEnd = countDate;
		List<Map<String, Object>> rankByToday = amazonorderMapper.countRankShopByAmountRmb(dayBegin,dayEnd);
		//昨日排名
		dayBegin = DateUtil.getYYYY_MM_DD(DateUtil.addDays(now, -1));
		dayEnd = DateUtil.getYYYY_MM_DD(DateUtil.addDays(now, -1));
		List<Map<String, Object>> rankByYesterday = amazonorderMapper.countRankShopByAmountRmb(dayBegin,dayEnd);
		//近7日排名排名
		dayBegin = DateUtil.getYYYY_MM_DD(DateUtil.addDays(now, -7));
		dayEnd = DateUtil.getYYYY_MM_DD(DateUtil.addDays(now, -1));
		List<Map<String, Object>> rankBySevenDay = amazonorderMapper.countRankShopByAmountRmb(dayBegin,dayEnd);
		//近30日排名
		dayBegin = DateUtil.getYYYY_MM_DD(DateUtil.addDays(now, -30));
		dayEnd = DateUtil.getYYYY_MM_DD(DateUtil.addDays(now, -1));
		List<Map<String, Object>> rankByThirtyDay = amazonorderMapper.countRankShopByAmountRmb(dayBegin,dayEnd);
		//前30日排名
		dayBegin = DateUtil.getYYYY_MM_DD(DateUtil.addDays(now, -60));
		dayEnd = DateUtil.getYYYY_MM_DD(DateUtil.addDays(now, -31));
		List<Map<String, Object>> rankByThirtyDayAgo = amazonorderMapper.countRankShopByAmountRmb(dayBegin,dayEnd);

		//获取所有店铺
		List<Long> allShopId = amazonorderMapper.selectShopId();

		List<TcoRankShopByAmountRmb> addList = new ArrayList<>();
		if(allShopId!=null && allShopId.size()>0){
			for(Long shopItem:allShopId){
				int rankToday = rankByToday!=null?rankByToday.size()+1:1;
				if(rankByToday!=null && rankByToday.size()>0){
					List<Map<String, Object>> rankByTodayByGuid = rankByToday.stream().filter(item->item.get("ShopId").toString().equals(shopItem.toString())).collect(Collectors.toList());
					if(rankByTodayByGuid.size()>0){
						rankToday = Integer.parseInt(rankByTodayByGuid.get(0).get("RowNumber").toString());
					}
				}
				int rankYesterday = rankByYesterday!=null?rankByYesterday.size()+1:1;
				if(rankByYesterday!=null && rankByYesterday.size()>0){
					List<Map<String, Object>> rankByYesterdayByGuid = rankByYesterday.stream().filter(item->item.get("ShopId").toString().equals(shopItem.toString())).collect(Collectors.toList());
					if(rankByYesterdayByGuid.size()>0){
						rankYesterday = Integer.parseInt(rankByYesterdayByGuid.get(0).get("RowNumber").toString());
					}
				}

				int rankSevenDay = rankBySevenDay!=null?rankBySevenDay.size()+1:1;
				if(rankBySevenDay!=null && rankBySevenDay.size()>0){
					List<Map<String, Object>> rankBySevenDayByGuid = rankBySevenDay.stream().filter(item->item.get("ShopId").toString().equals(shopItem.toString())).collect(Collectors.toList());
					if(rankBySevenDayByGuid.size()>0){
						rankSevenDay = Integer.parseInt(rankBySevenDayByGuid.get(0).get("RowNumber").toString());
					}
				}

				int rankThirtyDay = rankByThirtyDay!=null?rankByThirtyDay.size()+1:1;
				if(rankByThirtyDay!=null && rankByThirtyDay.size()>0){
					List<Map<String, Object>> rankByThirtyDayByGuid = rankByThirtyDay.stream().filter(item->item.get("ShopId").toString().equals(shopItem.toString())).collect(Collectors.toList());
					if(rankByThirtyDayByGuid.size()>0){
						rankThirtyDay = Integer.parseInt(rankByThirtyDayByGuid.get(0).get("RowNumber").toString());
					}
				}

				int rankThirtyDayAgo = rankByThirtyDayAgo!=null?rankByThirtyDayAgo.size()+1:1;
				if(rankByThirtyDayAgo!=null && rankByThirtyDayAgo.size()>0){
					List<Map<String, Object>> rankByThirtyDayAgoByGuid = rankByThirtyDayAgo.stream().filter(item->item.get("ShopId").toString().equals(shopItem.toString())).collect(Collectors.toList());
					if(rankByThirtyDayAgoByGuid.size()>0){
						rankThirtyDayAgo = Integer.parseInt(rankByThirtyDayAgoByGuid.get(0).get("RowNumber").toString());
					}
				}

				//填入参数
				TcoRankShopByAmountRmb tcoRankShopByAmountRmb = new TcoRankShopByAmountRmb();
				tcoRankShopByAmountRmb.setShopId(shopItem);
				tcoRankShopByAmountRmb.setStatus(1);
				tcoRankShopByAmountRmb.setRankToday(rankToday);
				tcoRankShopByAmountRmb.setRankYesterday(rankYesterday);
				tcoRankShopByAmountRmb.setRankSevenDay(rankSevenDay);
				tcoRankShopByAmountRmb.setRankThirtyDay(rankThirtyDay);
				tcoRankShopByAmountRmb.setRankThirtyDayAgo(rankThirtyDayAgo);
				tcoRankShopByAmountRmb.setCountDate(countDate);
				addList.add(tcoRankShopByAmountRmb);
			}
		}
		//logger.info("更新店铺每日店铺根据订单金额倒序的排名信息参数："+addList);
		//删除
		amazonorderMapper.deleteTcoRankShopByAmountRmbByCountDate(countDate);
		//分批插入,一次插入count数量
		int count = 50;
		for(int i=0;i<addList.size()+count;i=i+count){
			List<TcoRankShopByAmountRmb> newlist = addList.stream().skip(i).limit(count).collect(Collectors.toList());
			amazonorderMapper.insertTcoRankShopByAmountRmb(newlist);
		}
	}




}
